package com.gaogzhen.config.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import cn.hutool.core.bean.BeanUtil;


import java.util.Map;

/**
 * @author Administrator
 * @version 1.0
 * @description ali sms
 * @date 2022-09-30 11:19
 * 阿里云短信类
 */
public class AliSms {

    private final Client client;
    private final SendSmsRequest request;

    public AliSms(Client client, SendSmsRequest request) {
        this.client = client;
        this.request = request;
    }

    public  Map<String, Object> sendSms(String signName, String templateCode, String templateParam, String phoneNumbers) {

        request.setSignName(signName);
        request.setTemplateCode(templateCode);
        request.setTemplateParam(templateParam);
        request.setPhoneNumbers(phoneNumbers);

        RuntimeOptions runtime = new RuntimeOptions();
        SendSmsResponse response = null;
        try {
            response = client.sendSmsWithOptions(request, runtime);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("短信发送失败");
        }
        return BeanUtil.beanToMap(response.getBody());
    }
}
